package net.adetech.crj.biz;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import net.adetech.crj.utils.SerializeUtil;

import java.util.HashMap;
import java.util.Map;

public class ApiUtil {

    private static final String SYSTEM_BASE_URL = PropKit.get("SYSTEM_BASE_URL");;

    /**
     * 调用视瀚接口获取新号码
     * @param realName 姓名
     * @param idNumber 证件号码
     * @param serviceZone 服务区域
     * @param serviceQueue 服务队列
     * @return
     */
    public static JSONObject getNewNumber(String realName, String idNumber, String serviceZone, String serviceQueue)
    {


        idNumber = SerializeUtil.decodeBase64(idNumber);
        realName = SerializeUtil.decodeBase64(realName);



        String url = SYSTEM_BASE_URL + "TakeNoInterface";

        Map<String, String> params = new HashMap<>();
        params.put("real_name", realName==null?"":realName);
        params.put("identity_number", idNumber);
        params.put("service_zone", serviceZone);
        params.put("service_type", serviceQueue);
        String response = HttpUtils.get(url, params);
        JSONObject responseJson = JSONObject.parseObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
        return  responseJson;
    }

    public static JSONObject callNumber()
    {
        String url = SYSTEM_BASE_URL + "CallNoInterface";
        String response = HttpUtils.get(url);
        JSONObject responseJson = JSONObject.parseObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
        return responseJson;

    }

    public static JSONObject recallNumber(String no)
    {
        String url = SYSTEM_BASE_URL + "AnewCallNoInterface";

        Map<String, String> params = new HashMap<>();
        params.put("service_number", no);
        String response = HttpUtils.get(url,params);
        JSONObject responseJson = JSONObject.parseObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
        return responseJson;

    }

    /**
     * 调用视瀚修改号码状态接口
     * @param serviceNumber 号码
     * @param state 状态
     * @return 返回结果json
     */
    public static JSONObject updateNumberState(String serviceNumber,String state)
    {
        String url = SYSTEM_BASE_URL + "UpdateTakeNoState";
        Map<String, String> params = new HashMap<>();
        params.put("service_number", serviceNumber);
        params.put("service_state", state);
        String response = HttpUtils.get(url, params);
        JSONObject responseJson = JSONObject.parseObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
        return responseJson;
    }

    public static JSONObject getPhotoQueue()
    {
        String url = SYSTEM_BASE_URL + "PhotoWindowQueuing";
        Map<String, String> params = new HashMap<>();
        String response = HttpUtils.get(url, params);
        JSONObject responseJson = JSONObject.parseObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
        return responseJson;
    }
}
